/*
	tux study type - twindow.cpp
 	Copyright (c) 2003 by Linux_Lyb <linux_lyb@sohu.com>
 ____________________________________________________________________________	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

	See the file COPYING in the doc/ directory for more information.
*/

#include "twindow.h"

cairo_surface_t* image=NULL;

GdkPixmap *load_pixmap (GtkWidget *widget,gchar *filename)
{
GdkPixbuf *pixbuf=NULL;
GdkPixmap *pixmap=NULL;

	pixbuf = gdk_pixbuf_new_from_file (filename,NULL);
	pixmap = gdk_pixmap_new (widget->window,gdk_pixbuf_get_width(pixbuf),gdk_pixbuf_get_height(pixbuf),-1);
	
	gdk_draw_pixbuf(pixmap,
			widget->style->fg_gc[GTK_STATE_NORMAL],
			pixbuf,
			0,0,0,0,
			gdk_pixbuf_get_width(pixbuf),
			gdk_pixbuf_get_height(pixbuf),
			GDK_RGB_DITHER_NORMAL,0,0);


	/*
	gdk_pixbuf_render_to_drawable (pixbuf,pixmap,
			widget->style->fg_gc[GTK_STATE_NORMAL],
			0,0,0,0,
			gdk_pixbuf_get_width(pixbuf),
			gdk_pixbuf_get_height(pixbuf),
			GDK_RGB_DITHER_NORMAL,0,0);
	*/

	g_object_unref (pixbuf);
	return pixmap;
}

static gboolean on_window_expose_event(GtkWidget * widget,
	  GdkEventExpose * event, gpointer data)
{
	cairo_t *cr;

	cr = gdk_cairo_create(widget->window);
	cairo_set_source_surface(cr, image, 0, 0);
	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_paint(cr);
	cairo_destroy(cr);
	return FALSE;
}




void window_button_press (GtkWidget *window,GdkEventButton *event)
{
	if (event->button == 1 && event-> type == GDK_BUTTON_PRESS)
		gtk_window_begin_move_drag (GTK_WINDOW(window),event->button,
			(gint)event->x_root,(gint)event->y_root,event->time);
}

gboolean tWindow::create(const char *title,const char *pixmap_name)
{
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	if (window == NULL ) return FALSE;
		
	gtk_window_set_title (GTK_WINDOW(window),title);
	
	gtk_widget_set_events (window,GDK_SCROLL_MASK);
	gtk_window_set_decorated (GTK_WINDOW(window),FALSE);
	gtk_widget_set_app_paintable(window,TRUE);
	//gtk_window_set_resizable (GTK_WINDOW(window),FALSE);
	//gtk_widget_set_size_request (window,280,42);
	gtk_window_resize(GTK_WINDOW(window),280,42);
	gtk_window_set_keep_above(GTK_WINDOW(window),TRUE);
	//gtk_widget_set_size_request (window,163,36);
	gtk_window_set_position (GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
	//gtk_widget_realize (window);

	g_signal_connect (GTK_OBJECT(window),"button_press_event",
				G_CALLBACK (window_button_press),this);

	fixed = gtk_fixed_new();
	gtk_container_add (GTK_CONTAINER(window),fixed);
	gtk_widget_show (fixed);


	//setup the background
	set_twindow_bg (pixmap_name,fixed);
	
	g_signal_connect(G_OBJECT(window), "expose-event",
		  G_CALLBACK(on_window_expose_event), NULL);

	GdkScreen *screen;
	GdkColormap *colormap;

	screen = gtk_widget_get_screen(window);
	colormap = gdk_screen_get_rgba_colormap(screen);
	gtk_widget_set_colormap(window, colormap);


	gtk_widget_show_all (window);

	return TRUE;
}

gboolean tWindow::set_twindow_bg(const char *filename,GtkWidget *widget)
{	
	//clear_bg();
	//bg = load_pixmap (widget,filename);

	image = cairo_image_surface_create_from_png(filename);
	//gdk_window_set_back_pixmap (widget->window,bg,FALSE);
	//gdk_window_clear (widget->window);
	return TRUE;
}

tWindow::tWindow()
{
	bg = NULL;
	fixed = NULL;
	window = NULL;
	//image = NULL;
	
}

tWindow::~tWindow()
{
        if (bg)
	  g_object_unref (bg);

        if (fixed)
	  gtk_widget_destroy (fixed);

        if (window)
	  gtk_widget_destroy (window);
}
